package com.aco.party.util;

/**
 * Created by flying on 4/24/2016 0024.
 */
public class NavUtils {
    private static final int show_ = 10;

    private static final int sep_ = show_ / 2;

    private static final int split_ = sep_ / 2;

    /**
     *
     * @param offset 当前页数
     * @param limit 每页条数
     * @param total 总数据量
     * @return
     */
    public static String build(int offset, int limit, int total,String url) {
        if(total <= limit) return ""; //如果不足一页，则不显示分页控件
        int count = total % limit == 0 ? total / limit : total / limit + 1;

        StringBuffer buffer = new StringBuffer();

        // 判断是否显示上页
        if (offset > 1) {
            int prev = offset - 1;
            buffer.append(getNormalPart("<", prev, limit,url));
        }

        // 页数不超过限制的情况
        if (count <= show_)
            buffer.append(getPart(1, count, offset, limit,url));

        // 页数大于限制的情况

        if (count > show_) {
            if (offset <= sep_) {
                buffer.append(getPart(1, sep_ + split_, offset, limit,url));
                buffer.append(getEllipsis("...")).append(getNormalPart(String.valueOf(count), count, limit,url));
            } else if (offset > (count - sep_)) {
                buffer.append(getNormalPart(String.valueOf(1), 1, limit,url)).append(getEllipsis("..."));
                buffer.append(getPart(count - sep_ - 1, count, offset, limit,url));
            } else {
                buffer.append(getNormalPart(String.valueOf(1), 1, limit,url)).append(getEllipsis("..."));
                buffer.append(getPart(offset - split_ - 1, offset + split_ + 1, offset, limit,url));
                buffer.append(getEllipsis("...")).append(getNormalPart(String.valueOf(count), count, limit,url));
            }
        }

        // 判断是否显示下页
        if (offset < count) {
            int next = offset + 1;
            buffer.append(getNormalPart(">", next, limit,url));
        }

        return buffer.toString();

    }

    // 一般按钮
    private static StringBuffer getNormalPart(String content, int offset, int limit,String url) {
        StringBuffer buffer = new StringBuffer();
        buffer.append(String.format("<a href='%s%s' class='page-item' data-page='%s'>%s</a>&nbsp;",url,offset,offset,content));
//        buffer.append("<a href='javascript:void(0);' ").append("onclick='pageClick(").append(offset).append(",").append(limit).append(
//                ");'").append("'>").append(content).append("</a>");
        return buffer;
    }

    // 拼接中间部分
    private static StringBuffer getPart(int begin, int end, int offset, int limit,String url) {
        StringBuffer buffer = new StringBuffer();
        for (int i = begin; i <= end; i++) {
            if (offset == i)
                buffer.append(getSelectedPart(String.valueOf(i), i));
            else
                buffer.append(getNormalPart(String.valueOf(i), i, limit,url));
        }
        return buffer;
    }

    // 选中按钮
    private static StringBuffer getSelectedPart(String content, Integer value) {
        StringBuffer buffer = new StringBuffer();
        buffer.append(String.format("<a href='javascript:void(0);' class='act'>%s</a>&nbsp;",content));
//        buffer.append("<a href='javascript:void(0);'").append("' class='current'>").append(content).append("</a>");
        return buffer;
    }

    // 省略部分
    private static StringBuffer getEllipsis(String content) {
        StringBuffer buffer = new StringBuffer();
        buffer.append("<a href='javascript:void(0);' class='page-item'>").append(content).append("</a>&nbsp;");
        return buffer;
    }

    public static void main(String[] args) {
        System.out.println(build(2, 10, 101,"http://morketing/slug/news/page/"));
    }
}
